﻿<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');
/* ************************************************** *\
 * @company		: NovaITech							  *	
 * @author		: van.vo@novaitech.com				  * 
 * @copyright	: 2012								  * 
 * ================================================== *
 * @date		: 04/26/2012
 * @project		: Drag & Drop
 * @description	: 
 *		- 
 *
\* ************************************************** */

class Infos extends MY_Controller{
    function __construct( ){
        parent::__construct( );  
		$this->load->helper( array( 'form', 'url' ) );
		$this->load->model( 'CodeGenerator_model', '', TRUE );
		$this->load->helper('date');
		$this->load_template();
    }
	
	function index(){
        //paging
		$filter = $this->input->get('filter');
		$orderby = $this->input->get('orderby');
        $config['base_url'] = base_url().'customer/manage/';
		if($filter) {$config['suffix'] = "/?filter=$filter&orderby=$orderby";
			$config['first_url'] = $config['base_url']."0/?filter=$filter&orderby=$orderby";
		}
		
		// filter danh muc

		$combobox_cats['tuan'] = "tuan";
		$combobox_cats['thang'] = "thang";
		
		$selected_cat=$this->input->post('info');
		if(empty($selected_cat)) $selected_cat=$this->input->get('info');
		if(empty($selected_cat)) $selected_cat = 'tuan';	
		$this->data['combobox_cats']=form_dropdown('info', $combobox_cats,$selected_cat);
		//end

        $config['total_rows'] = $this->CodeGenerator_model->count("customer");
        $config['per_page'] = PER_PAGE;	
        $this->pagination->initialize($config); 
		$start = $this->uri->segment(3);
		if(empty($start)) $start = 0;
		$perpage = $config['per_page'];
		if(empty($filter)) $str_orderby = ''; else $str_orderby = "$filter $orderby";
        // Get rules from config file
        // --------------------------------------------------
        //$rules                  =   $this->form_validation->_config_rules[ 'customer_add' ];
        $rules                  =         array(array('field_id' => 'F_Name','label' => 'Tên','control' => 'Array',),array('field_id' => 'L_Name','label' => 'Họ','control' => 'Array',),array('field_id' => 'DOB','label' => 'Ngày sinh','control' => 'array',),array('field_id' => 'birthdays','label' => 'Con lai','control' => 'day',),array('field_id' => 'Phone_NO','label' => 'Số ĐT','control' => 'Array',),array('field_id' => 'Street','label' => 'Đường','control' => 'Array',),array('field_id' => 'District_City','label' => 'Tp','control' => 'Array',),array('field_id' => 'Membership_Type','label' => 'Loại thành viên','control' => 'Array',),array('field_id' => 'Email_Addr','label' => 'Email','control' => 'Array',));
        
       
        // Make header
        // --------------------------------------------------
        $header = array( );
        foreach( $rules as $k => $item ){
             $header[ ] = $item[ 'label' ];
			$str1='';$str2='';
			if($filter == $item['field_id']){
				if($orderby == "DESC") {$orderby = "ASC";$img1="display: none;";$img2="display: inline;"; } else {$orderby = "DESC";$img1="display: inline;";$img2="display: none;";}
				$url= base_url()."customer/manage/$start/?filter=$filter&orderby=$orderby";
				$str1 = 'onmouseout="$(\'.soimg2\').toggle()" onmouseover="$(\'.soimg2\').toggle()"';
				$str2='<img class="icon ic_s_asc soimg2" alt="Ascending" title="" src="'.base_url().'resources/images/dot.gif" style="'.$img1.'"> <img class="icon ic_s_desc soimg2 hide" alt="Descending" title="" src="'.base_url().'resources/images/dot.gif" style="'.$img2.'">';
			}else $url= base_url()."customer/manage/$start/?filter=".$item['field_id']."&orderby=ASC";
			$rules[$k]['html'] ='<span><a title="" '.$str1.' href="'.$url.'">'.$item[ 'label' ].' '.$str2.'</a></span>';
			if(isset($item['order'])&&!$item['order']) $rules[$k]['html'] = $item[ 'label' ];
        }
		$this->data[ 'rules' ]  =   $rules;
	   
        $this->data[ 'header' ] =   $header;
		if($selected_cat=="tuan") $day = 7;
		else $day=30;
		if(!empty($str_orderby)) $str_orderby = ",$str_orderby";
		$sql= "SELECT *, FLOOR( DATEDIFF( DATE( NOW( ) ) , STR_TO_DATE( DOB, '%d/%m/%Y' ) ) / 365.25 ) AS age_now, DATEDIFF(CONCAT(CONCAT(EXTRACT(YEAR FROM CURDATE()),'-'), DATE_FORMAT(STR_TO_DATE( DOB, '%d/%m/%Y' ), '%m-%d')), CURDATE()) AS birthdays
FROM customer
WHERE 1 = ( FLOOR( DATEDIFF( DATE_ADD( DATE( NOW( ) ) , INTERVAL $day
DAY ) , STR_TO_DATE( DOB, '%d/%m/%Y' ) ) / 365.25 ) ) - ( FLOOR( DATEDIFF( DATE( NOW( ) ) , STR_TO_DATE( DOB, '%d/%m/%Y' ) ) / 365.25 ) )
ORDER BY MONTH( STR_TO_DATE( DOB, '%d/%m/%Y' ) ) , DAY( STR_TO_DATE( DOB, '%d/%m/%Y' ) ) $str_orderby";
		// die($sql);
       // $results                =   $this->CodeGenerator_model->get( 'customer', 'Cust_ID,F_Name,L_Name,DOB,Phone_NO,Street,District_City,Membership_Type,Email_Addr,Create_Dt,Delete','', $config['per_page'],$start,false,"array", $str_orderby );
	   $results= $this->CodeGenerator_model->custom_query($sql,"array");
        
        // Make action Edit/Delete
        // --------------------------------------------------
        for( $i = 0; $i < count( $results ); $i++ ){
	       $id = array_values( $results[ $i ]);
            $results[ $i ][ 'send'   ]   =  anchor( '#' , 'Gui tn');                      
        }
        
        $this->data[ 'results'        ]     =   $results;
        $this->data[ 'master_limited' ]     =   explode(',', 'Cust_ID,F_Name,L_Name,DOB,Phone_NO,Street,District_City,Membership_Type,Email_Addr,Create_Dt,Delete,birthdays');
        $this->data[ 'action'         ]     =   array(array('id' => 'send','label' => 'Gui tn','url' => 'index.php/customer/edit/',));
		// $this->load->view( 'employee_list', $this->data ); 
		$this->template->write( 'title', 'Spa - Thong bao sinh nhat' );
		// $this->template->add_css( 'resources/css/users.css' );
        $this->template->write_view( 'content', 'infos.php',$this->data );
        $this->template->render();
    } 
	
}

/* End of file index.php */
/* Location: ./index.php */